﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />
/// <reference path="../Scripts/util.js" />
/// <reference path="../Scripts/Member.js" />


$.ajaxSetup({
    type: "POST",
    dataType: "text",
    global: false
});

var leader;
var member;
var attachment;
var projectID;
var category;
var userID;
var empIDs;
var tableID = "1";
function Init() {
    leader = new Member(
        document.getElementById("parentLeaderContainer"),
        document.getElementById("hdLeaderCount"),
        document.getElementById("divNewLeader_0"),
        "divNewLeader_",
        "removeLeader_",
        "NoLeader_",
        this.projectID,
        this.userID,
        this.tableID);

    member = new Member(
        document.getElementById("parentMemberContainer"),
        document.getElementById("hdMemberCount"),
        document.getElementById("divNewMember_0"),
        "divNewMember_",
        "removeMeber_",
        "NoMember_",
        this.projectID,
        this.userID,
        this.tableID);

    attachment = new ProjectAttachment(
        document.getElementById("parentFileContainer"),
        document.getElementById("hdFileCount"),
        document.getElementById("divNewFile_0"),
        "divNewFile_",
        "removeFile_",
        "NoFile_",
        this.projectID,
        this.userID,
        this.tableID);
}

$(document).ready(function () {
    category = $("#MainContent_Category").val();
    projectID = $("#MainContent_ID").val();
    userID = $("#MainContent_UserID").val();
    Init();
    BindDepartment();
    $("#btnAddLeader").bind("click", AddLeader);
    $("#btnAddMember").bind("click", AddMember);
    $("#btnSave").bind("click", Back);
    $("#btnBack").bind("click", Back);
    $("#btnUpload").bind("click", ajaxFileUpload);
    $("#DepLeader").change(function () {
        BindEmployee($("#DepLeader").find('option:selected').val(), "#EmpLeader");
    });
    $("#DepMember").change(function () {
        BindEmployee($("#DepMember").find('option:selected').val(), "#EmpMember");
    });
    this.empIDs = new Array();
    LoadMembers();
    LoadAttchments();
});

function ajaxFileUpload() {
    var filePath = $("#file_upload").val()
    var fileName = filePath.replace(/.*(\/|\\)/, "");
    var fileType=$("#FileType").find('option:selected').val();
    var fileExt = (/[.]/.exec(fileName)) ? /[^.]+$/.exec(fileName.toLowerCase()) : '';
    var exts = new Array("doc", "xls", "zip", "rar", "jpg", "jpeg", "gif", "pdf");
    if (!exts.contains(fileExt)) {
        alert('不支持文件格式:' + fileExt);
        return;
    }

    $.ajaxFileUpload
    (
        {
            url: '/Handler/Attachment.ashx?' + (new Date()).valueOf(),
            secureuri: false,
            fileElementId: 'file_upload',
            contentType: 'application/x-www-form-urlencoded;charset=gb2312',
            data: { action: "addData", TableID: "1", ID: encodeURIComponent(projectID), LastModifiedBy: encodeURIComponent(userID), FileType: encodeURIComponent(fileType) },
            success: function (data, status) {
                if (typeof (data.error) != 'undefined') {
                    alert('上传文件失败!');
                }
                else {
                    var ret = attachment.addMember(fileName, fileType);
                    if (!ret) {
                        alert('已经上传相同文件名!');
                    }
                }
                Util.unblockUI();
            },
            error: function (data, status, e) {
                alert(e);
                Util.unblockUI();
            }
        }
    )

        Util.blockUI("正在上传文件...");

    return false;
}

function Back() {
    Util.freshPage("ProjectManage.aspx?ID=" + projectID+"&Category="+category);
}

function BindDepartment() {
    $.ajax({
        url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData" },
        success: function (data) {
            var ret = $.parseJSON(data).Departments;
            var depLeader = $("#DepLeader");
            var depMember = $("#DepMember");
            depLeader.empty();
            depMember.empty();
            depLeader.append("<option value='-1'>-请选择-</option>");
            depMember.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                depLeader.append("<option value='" + decodeURIComponent(ret[i].ID) + "'>" + decodeURIComponent(ret[i].Name) + "</option>");
                depMember.append("<option value='" + decodeURIComponent(ret[i].ID) + "'>" + decodeURIComponent(ret[i].Name) + "</option>");
            }
        } 
    });
}

function BindEmployee(depID, emp) {
    $.ajax({
        url: "/Handler/Employee.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadDataByDepID", depID: depID },
        success: function (data) {
            var ret = $.parseJSON(data).Employees;
            var employees = $(emp);
            employees.empty();
            employees.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                var value = decodeURIComponent(ret[i].EmpID) + "|" + decodeURIComponent(ret[i].EmpName) + "|" + decodeURIComponent(ret[i].DepName)
                employees.append("<option value='" + value + "'>" + decodeURIComponent(ret[i].EmpName) + "</option>");
            }
        }
    });
}

function LoadMembers() {
    $.ajax({
        url: "/Handler/Member.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData", TableID: "1", ID: projectID },
        beforeSend: function () {
            Util.blockUI("正在加载数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var ret = $.parseJSON(data).Members;
            for (var i = 0; i < ret.length; i++) {
                var memberInfo = ret[i];
                if (memberInfo.MemberType == 1 || memberInfo.MemberType == 2) {
                    leader.addMember(memberInfo.EmpID, memberInfo.EmpName, memberInfo.DepName, memberInfo.MemberType);
                }
                else {
                    member.addMember(memberInfo.EmpID, memberInfo.EmpName, memberInfo.DepName, memberInfo.MemberType);
                }
            }
        }
    });
}

function LoadAttchments() {
    $.ajax({
        url: "/Handler/Attachment.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData",TableID:"1", ID: projectID },
        success: function (data) {
            var ret = $.parseJSON(data).Attachments;
            for (var i = 0; i < ret.length; i++) {
                attachment.addMember(decodeURIComponent(ret[i].FileName), decodeURIComponent(ret[i].FileType));
            }
        }
    });
}

function AddLeader() {
    var emp = $("#EmpLeader").find('option:selected').val();
    if (emp == undefined) {
        return;
    }
    var arr = emp.split('|');
    var memberType = $("#LeaderType").find('option:selected').val();
    if (arr[0] != -1) {
        var exist = leader.addMember(arr[0], arr[1], arr[2], memberType);
        if (!exist) {
            alert('研究人员已经添加!');
        }
        else {
            AddProjectMember(arr[0], memberType);
        }
    }
}

function AddMember() {
    var emp = $("#EmpMember").find('option:selected').val();
    if (emp == undefined) {
        return;
    }

    var arr = emp.split('|');
    var memberType = $("#MemberType").find('option:selected').val();
    if (arr[0] != -1) {
        var exist = member.addMember(arr[0], arr[1], arr[2], memberType);
        if (!exist) {
            alert('研究人员已经添加!');
        }
        else {
            AddProjectMember(arr[0], memberType);
        }
    }
}

function AddProjectMember(empID, memberType) {
    var postData =
    {
        "TableID":"1",
        "ID": encodeURIComponent(projectID),
        "EmployeeID": encodeURIComponent(empID),
        "MemberType": encodeURIComponent(memberType),
        "LastModifiedBy": encodeURIComponent(userID)
    };
    var jsonStr = $.toJSON(postData);
    $.ajax({
        url: "/Handler/Member.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "addData", jsonDataString: jsonStr },
        success: function (data) {
        }
    });
}